package com.ldy.seal.vo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.springframework.util.StringUtils;

import com.nja.util.encoders.Base64;

/**
 * pdf签章
 * 
 * @author lindeyi
 *
 */
public class Seals {

	/** 印章图片*/
	public static final String FILE_NAME = "seal.png";
	/** 签章分组*/
	public static final String GROUP = "seal";
	
	/** 文档id*/
	private String id;

	/**
	 * 证书文件base64
	 */
	private String cert;

	/**
	 * 印章图片base64
	 */
	private String sealStr;
	
	/**
	 * 印章图片
	 */
	private byte[] seal;

	/**
	 * 签章位置
	 */
	private List<SealInfo> seals;

	/**
	 * 骑缝章
	 */
	private SealInfo paging;
	
	/**
	 * 获取印章
	 * @return byte[]
	 */
	public byte[] getSealImg(String sealSavepath) {
		if (null != seal) {
			return seal;
		}
		if (StringUtils.hasText(sealStr)) {
			seal = Base64.decode(sealStr);
			return seal;
		}
		String serialNumber = null;
		try {
			ByteArrayInputStream bio = new ByteArrayInputStream(Base64.decode(cert));
			CertificateFactory cf = CertificateFactory.getInstance("X.509");
			X509Certificate certificate = (X509Certificate) cf.generateCertificate(bio);
			serialNumber = certificate.getSerialNumber().toString(16);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null == serialNumber) {
			return null;
		}
		
		File dir = new File(sealSavepath, serialNumber);
		if (!dir.exists()) {
			return null;
		}
		try (InputStream io = new FileInputStream(new File(dir, Seals.FILE_NAME));
				ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
			IOUtils.copy(io, bos);
			seal = bos.toByteArray();
		} catch (Exception e) {}
        return seal;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the cert
	 */
	public String getCert() {
		return cert;
	}

	/**
	 * @param cert the cert to set
	 */
	public void setCert(String cert) {
		this.cert = cert;
	}

	/**
	 * @return the sealStr
	 */
	public String getSealStr() {
		return sealStr;
	}

	/**
	 * @param sealStr the sealStr to set
	 */
	public void setSealStr(String sealStr) {
		this.sealStr = sealStr;
	}

	/**
	 * @return the seal
	 */
	public byte[] getSeal() {
		return seal;
	}

	/**
	 * @param seal the seal to set
	 */
	public void setSeal(byte[] seal) {
		this.seal = seal;
	}

	/**
	 * @return the seals
	 */
	public List<SealInfo> getSeals() {
		return seals;
	}

	/**
	 * @param seals the seals to set
	 */
	public void setSeals(List<SealInfo> seals) {
		this.seals = seals;
	}

	/**
	 * @return the paging
	 */
	public SealInfo getPaging() {
		return paging;
	}

	/**
	 * @param paging the paging to set
	 */
	public void setPaging(SealInfo paging) {
		this.paging = paging;
	}

	/**
	 * @return the fileName
	 */
	public static String getFileName() {
		return FILE_NAME;
	}

	/**
	 * @return the group
	 */
	public static String getGroup() {
		return GROUP;
	}

}
